package model.algorithm;

import model.datastructure.FieldSurface2D;

public class BestFitAlgorithm extends AbstractAlgorithm {

	/**
	 * 
	 * @param field
	 * @param iterator
	 * @param lookahead
	 */
	public BestFitAlgorithm( AbstractSurfaceIterator iterator ) {
		super( iterator );
		name = "BestFitAlgorithm";
	}

	/**
	 * 
	 */
	public double evaluate( FieldSurface2D surface, int[] emptySpace ) {
		/* get height of the new surface */
		double height = surface.getHighest();

		int emptyWeight = evaluateEmptySpace( emptySpace );

		/* height to the power 2 to let it grow faster than empty space
		 * emptyspace is multiplied by the width of the surface, this makes that
		 * the height weighs more on small fields
		 */
		double evaluation = Math.pow( height, 4 ) + mSurfaceIterator.getFieldWidth() * emptyWeight;
		//double evaluation = Math.pow( height, 1.3 ) + 5 * emptyWeight;

		return evaluation;
	}

	/**
	 * Walks through the emptySpace array and creates a weight out of the values
	 * 
	 * @param emptySpace
	 * @return int weight of the emptySpace
	 */
	public int evaluateEmptySpace( int[] emptySpace ) {
		int iWeight = 0;

		for( int iSpace = 0; iSpace < emptySpace.length; iSpace ++ ) {
			iWeight += emptySpace[ iSpace ];
		}

		return iWeight;
	}

}